package com.myccb.app.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
 * Created by hyt on 2017/12/10.
 */

public class DateTimeUtil {
//    凌晨(1：00—5：00)
//    早上(5：00—8：00)
//    上午(8：00—11：00)
//    中午(11：00—13：00)
//    下午(13：00—17：00)
//    晚上(17：00—23：00)
//    半夜(23：00—1：00)
    public static String[] periodOfDay={"凌晨","早上","上午","中午","下午","晚上","半夜"};

    public static String getCurrentDateTime(String format){
        SimpleDateFormat formatter=new SimpleDateFormat(format);
        Date curDate =  new Date(System.currentTimeMillis());
        return formatter.format(curDate);
    }


    public static String addDateTime(String srcTime,String format,int year,int month,int day){

        SimpleDateFormat f = new SimpleDateFormat(format);

        Date datet = null;

        try {
            datet=f.parse(srcTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(datet);
        calendar.add(Calendar.YEAR,year);//年
        calendar.add(Calendar.MONTH,month);//月
        calendar.add(Calendar.DAY_OF_YEAR,day);//天
        Date dt1=calendar.getTime();
        String reStr = f.format(dt1);

        return reStr;
    }

    public static boolean isTimeInTimePeriod(String sourceTime, String curTime) {
        if (sourceTime == null || !sourceTime.contains("-") || !sourceTime.contains(":")) {
            throw new IllegalArgumentException("Illegal Argument arg:" + sourceTime);
        }
        if (curTime == null || !curTime.contains(":")) {
            throw new IllegalArgumentException("Illegal Argument arg:" + curTime);
        }
        String[] args = sourceTime.split("-");
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
        try {
            long now = sdf.parse(curTime).getTime();
            long start = sdf.parse(args[0]).getTime();
            long end = sdf.parse(args[1]).getTime();
            if (args[1].equals("00:00")) {
                args[1] = "24:00";
            }
            if (end < start) {
                if (now >= end && now < start) {
                    return false;
                } else {
                    return true;
                }
            }
            else {
                if (now >= start && now < end) {
                    return true;
                } else {
                    return false;
                }
            }
        } catch (ParseException e) {
            e.printStackTrace();
            throw new IllegalArgumentException("Illegal Argument arg:" + sourceTime);
        }
    }
    public static String getCurrentPeriodOfDay(){
        SimpleDateFormat formatter=new SimpleDateFormat("HH:mm");
        Date curDate =  new Date(System.currentTimeMillis());
        String currentTime=formatter.format(curDate);
        if (isTimeInTimePeriod("01:00-5:00",currentTime))
            return periodOfDay[0];
        if (isTimeInTimePeriod("05:00-08:00",currentTime))
            return periodOfDay[1];
        if (isTimeInTimePeriod("08:00-11:00",currentTime))
            return periodOfDay[2];
        if (isTimeInTimePeriod("11:00-13:00",currentTime))
            return periodOfDay[3];
        if (isTimeInTimePeriod("13:00-17:00",currentTime))
            return periodOfDay[4];
        if (isTimeInTimePeriod("17:00-23:00",currentTime))
            return periodOfDay[5];
        if (isTimeInTimePeriod("23:00-01:00",currentTime))
            return periodOfDay[6];
        return "异时间";
    }
    public static String dateToWeek(String datetime) {
        SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
        //String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
        String[] weekDays = { "周日", "周一", "周二", "周三", "周四", "周五", "周六" };
        Calendar cal = Calendar.getInstance(); // 获得一个日历
        Date datet = null;
        try {
            datet = f.parse(datetime);
            cal.setTime(datet);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1; // 指示一个星期中的某天。
        if (w < 0)
            w = 0;
        return weekDays[w];
    }


}
